﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Timers;

namespace AsicMonitor
{
    public partial class SettingProgress : Form
    {
        private Conf conf;
        private List<Miner> miners;
        private System.Timers.Timer barTimer;
        private Dictionary<Miner, Conf> minerConfMap;

        public SettingProgress(Conf conf, List<Miner> miners)
        {
            this.miners = miners;
            this.conf = conf;

            InitializeComponent();

            init();
        }

        private void init()
        {
            minerConfMap = new Dictionary<Miner, Conf>();

            this.refreshBarHandler += this.OnRefreshBar;
            minerConfigPar.Maximum = miners.Count;
            minerConfigPar.Value = 0;

            detailRichTxt.Enabled = false;
            detailRichTxt.Multiline = true;     //将Multiline属性设置为true，实现显示多行
            detailRichTxt.ScrollBars = RichTextBoxScrollBars.Vertical; //设置ScrollBars属性实现只显示垂直滚动


            // send websocket
            int idx = 1;
           
            foreach (Miner miner in miners)
            {
                Conf new_conf = miner.Conf;

                string new_name = miner.Conf != null ? miner.Conf.miner_name : "";
                new_name = conf.miner_name != null ? conf.miner_name : new_name;

               new_conf.miner_name = $"{new_name}_{idx.ToString().PadLeft(3, '0')}";
                new_conf.url = conf.url;
                new_conf.user = conf.user;
                if(new_conf.user != null && !new_conf.user.Contains("."))
                {
                    new_conf.user = $"{new_conf.user}.{new_conf.miner_name}";       // 自动增加上矿机名字
                }
                // 加上区分参数
                new_conf.pass = conf.pass;
                new_conf.algo = conf.algo != null ? conf.algo : "lyra2z";

                minerConfMap.Add(miner, new_conf);

                miner.Conf = null;
                miner.sendSetConfMsg(new_conf);
                idx++;
            }

            // start to refresh 
            barTimer = new System.Timers.Timer(3000);
            barTimer.Elapsed += new ElapsedEventHandler((sender, e) => {
                foreach(Miner miner in miners)
                {
                    miner.trySendGetConfIfConfNull();
                }

                this.Invoke(new RefreshBar(OnRefreshBar), null);
            });
            barTimer.AutoReset = true;
            barTimer.Enabled = true;
        }

        public delegate void RefreshBar();
        private RefreshBar refreshBarHandler;


        private void OnRefreshBar()
        {
            int okCount = 0;
            string done = "";
            string waiting = "";

            foreach (Miner miner in miners)
            {
                Conf new_conf = minerConfMap[miner];
                if (miner.checkConfMatch(new_conf))
                {
                    okCount++;
                    done += miner.IP + ":" + miner.Conf.miner_name + "  ok\n";
                }
                else
                {
                    waiting += miner.IP + ":" + miner.Conf.miner_name + "  waiting\n";
                }
            }
            minerConfigPar.Value = okCount;

            detailRichTxt.Text = done + "\n-------------------------------------------\n\n" + waiting;

            if (okCount == miners.Count)
            {
                barTimer.Stop();
                barTimer.Close();

                MessageBoxEx.Show(this, "设置完毕");
                Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void minerConfigPar_Click(object sender, EventArgs e)
        {

        }


    }
}
